package com.zcsy.manage.service.system.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.ehcache.CacheConstants;
import com.zcsy.commons.ehcache.CacheUtils;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.model.system.DictVo;
import com.zcsy.manage.persistence.system.DictDao;
import com.zcsy.manage.service.system.IDictService;

/**
 * 
 * @Title: DictServiceImpl.java
 * @Package： com.zcsy.service.system.impl 
 * @Description: 数据字典服务接口实现类
 * @author chenshengfang 735569870@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年7月6日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class DictServiceImpl extends SystemComService implements IDictService {
	
    @Autowired
    private DictDao dictDao;

    @Override
    public int insert(Dict record) {
        int i;
        i = dictDao.insert(record);
        putDictToCache();
        return i;
    }

    @Override
    public Dict selectByPrimaryKey(String dicId) {
        Dict dict = dictDao.selectByPrimaryKey(dicId);
        setMessage(dict);
        
        return dict;
    }
    
    @Override
    public List<Dict> selectByType(String type) {
        return dictDao.selectByType(type);
    }
    
    @Override
    public List<Dict> getDictList() {
        List<Dict> list = dictDao.getDictList();
        
        return list;
    }
    
    @Override
    public List<Dict> queryDict(Dict dict) {
        return dictDao.queryDict(dict);
    }

    @Override
    public int updateByPrimaryKeySelective(Dict record) {
        int i = dictDao.updateByPrimaryKeySelective(record);
        putDictToCache();
        return i;
    }

    @Override
    public int deleteByPrimaryKey(String dicId) {
        return dictDao.deleteByPrimaryKey(dicId);
    }
    
    @Override
    public Dict selectByFilter(Integer value, String type) {
        return dictDao.selectByFilter(value, type);
    }

    /**
     * 
     * @Description <br>
     * 为每一个数据类型设置信息
     * @param dict 数据字典
     * @author chenshengfang 735569870@qq.com
     * @date 2015年7月13日
     * @see
     */
    private void setMessage(Dict record){
        List<DictVo> dictVoList = new ArrayList<DictVo>();
        List<Dict> dictList = dictDao.selectByType(record.getType());
        for(Dict dict : dictList){
            DictVo dictVo = new DictVo();
            dictVo.setDictId(dict.getId());
            dictVo.setLabel(dict.getLabel());
            dictVo.setSort(dict.getSort());
            dictVo.setValue(dict.getValue());
            
            dictVoList.add(dictVo);
        }
        
        record.setDictVoList(dictVoList);
    }

    @Override
    public int deleteByType(String type) {
        return dictDao.deleteByType(type);
    }
    
    /**
     * 
     * @Description <br>
     * 刷新数据字典缓存方法
     * @author chenshengfang 735569870@qq.com
     * @date 2015年7月18日
     * @see
     */
    public void putDictToCache(){
        List<Dict> list = dictDao.getDictList();
        Set<String> typeSet = new HashSet<String>();
        for(Dict dict : list){
            typeSet.add(dict.getType());
        }
        for(String type : typeSet){
            List<Dict> dictList = dictDao.selectByType(type);
             CacheUtils.put(type, dictList);
        }
    }

	@Override
    public List<Dict> getDictFromCache(String type) {
        List<Dict> list = new ArrayList<Dict>();
        
        list = (List<Dict>)CacheUtils.get(CacheUtils.SYS_CACHE, CacheConstants.SYS_DICT + type);
        if(CollectionUtils.isEmpty(list)){
        	list = selectByType(type);
        	
        	if(CollectionUtils.isNotEmpty(list)){
        		CacheUtils.put(CacheConstants.SYS_DICT + type, list);
        	}
        }
        
        return list;
    }

    @Override
    public List<Dict> selectByDescription(String description) {
        return dictDao.selectByDescription(description);
    }
}
